@inherits AliasVault.Client.Auth.Pages.Base.LoginBase
@using System.Timers
@using Microsoft.Extensions.Localization

<div class="w-full mx-auto">
   @if (_isLoading)
    {
        <div class="absolute inset-0 flex justify-center items-center z-10">
            <div class="animate-spin rounded-full h-16 w-16 border-t-2 border-b-2 border-primary-500"></div>
        </div>
    }
    <div class="@(_isLoading ? "invisible opacity-0" : "opacity-100") transition-opacity duration-300 w-full">
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow-lg lg:shadow-none p-6">
            <div class="flex items-start mb-4">
                <div class="flex-shrink-0">
                    <img class="h-10 w-10 rounded-full" src="/img/avatar.webp" alt="AliasVault Assistant">
                </div>
                <div class="ml-3 bg-blue-100 dark:bg-blue-900 rounded-lg p-3">
                    <p class="text-sm text-gray-900 dark:text-white">
                        @Localizer["WelcomeMessage"]
                    </p>
                </div>
            </div>
        </div>

        <div class="p-4 mb-6 bg-gray-100 dark:bg-gray-900 rounded-lg text-gray-900 dark:text-gray-100">
            <p class="text-sm font-semibold">
                @Localizer["ImportantNote"]
            </p>
            <ul class="text-sm mt-3 list-disc list-inside">
                <li>@Localizer["SecurityPoint1"]</li>
                <li>@Localizer["SecurityPoint2"]</li>
                <li>@Localizer["SecurityPoint3"]</li>
            </ul>
        </div>

        <div class="space-y-4">
            <div>
                <div class="">
                    <EditFormRow Id="password" Label="@Localizer["MasterPasswordLabel"]" @bind-Value="Password" Type="password" Placeholder="@Localizer["MasterPasswordPlaceholder"]" OnFocus="@OnPasswordInputFocus"/>
                </div>
                <div class="mt-4">
                    <EditFormRow Id="confirmPassword" Label="@Localizer["ConfirmMasterPasswordLabel"]" @bind-Value="ConfirmPassword" Type="password" Placeholder="@Localizer["ConfirmMasterPasswordPlaceholder"]" OnFocus="@OnPasswordInputFocus" />
                </div>
                @if (_isValidating)
                {
                    <div class="mt-2 text-sm text-gray-600 dark:text-gray-400">@Localizer["ValidatingPasswordMessage"]</div>
                }
                else if (_isValid)
                {
                    @if (!string.IsNullOrEmpty(_errorMessage))
                    {
                        <div class="mt-2 text-sm text-yellow-600 dark:text-yellow-400">@_errorMessage</div>
                    }
                    else
                    {
                        <div class="mt-2 text-sm text-green-600 dark:text-green-400">@Localizer["PasswordValidAndStrongMessage"]</div>
                    }
                }
                else if (!string.IsNullOrEmpty(_errorMessage))
                {
                    <div class="mt-2 text-sm text-red-600 dark:text-red-400">@_errorMessage</div>
                }
            </div>
        </div>
    </div>
</div>

@code {
    private IStringLocalizer Localizer => LocalizerFactory.Create("Components.Auth.Setup.PasswordStep", "AliasVault.Client");

    /// <summary>
    /// The event callback for when the password changes.
    /// </summary>
    [Parameter]
    public EventCallback<string> OnPasswordChange { get; set; }

    private string Password
    {
        get => _password;
        set
        {
            if (_password != value)
            {
                _password = value;
                ValidatePassword();
            }
        }
    }

    private string ConfirmPassword
    {
        get => _confirmPassword;
        set
        {
            if (_confirmPassword != value)
            {
                _confirmPassword = value;
                ValidatePassword();
            }
        }
    }

    private string _password = string.Empty;
    private string _confirmPassword = string.Empty;
    private bool _isValid = false;
    private bool _isValidating = false;
    private string _errorMessage = string.Empty;
    private Timer? _debounceTimer;

    private bool _isLoading = true;
    private Timer? _loadingTimer;

    /// <inheritdoc />
    protected override void Dispose(bool disposing)
    {
        _loadingTimer?.Dispose();
        _debounceTimer?.Dispose();
        base.Dispose(disposing);
    }

    /// <inheritdoc />
    protected override void OnInitialized()
    {
        _loadingTimer = new Timer(300);
        _loadingTimer.Elapsed += (sender, e) => FinishLoading();
        _loadingTimer.AutoReset = false;
        _loadingTimer.Start();

        _debounceTimer = new Timer(300);
        _debounceTimer.Elapsed += async (sender, e) => await ValidatePasswordDebounced();
        _debounceTimer.AutoReset = false;
    }

    /// <inheritdoc />
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        if (firstRender)
        {
            await Task.Delay(100); // Give time for the DOM to update
            await JsInteropService.FocusElementById("password");
        }
    }

    /// <summary>
    /// Finishes the loading animation.
    /// </summary>
    private void FinishLoading()
    {
        _isLoading = false;
        InvokeAsync(StateHasChanged);
    }

    /// <summary>
    /// Validates the password immediately.
    /// </summary>
    private void ValidatePassword()
    {
        _isValidating = true;
        _isValid = false;
        _errorMessage = string.Empty;
        StateHasChanged();

        _debounceTimer?.Stop();
        _debounceTimer?.Start();
    }

    /// <summary>
    /// Validates the password after input has stopped.
    /// </summary>
    private async Task ValidatePasswordDebounced()
    {
        await InvokeAsync(async () =>
        {
            if (Password.Length < 10)
            {
                _isValidating = false;
                _isValid = false;
                _errorMessage = Localizer["PasswordTooShortError"];
                await OnPasswordChange.InvokeAsync(string.Empty);
                StateHasChanged();
                return;
            }

            if (string.IsNullOrWhiteSpace(ConfirmPassword))
            {
                _isValidating = false;
                _isValid = false;
                _errorMessage = string.Empty;
                await OnPasswordChange.InvokeAsync(string.Empty);
                StateHasChanged();
                return;
            }

            if (Password != ConfirmPassword)
            {
                _isValidating = false;
                _isValid = false;
                _errorMessage = Localizer["PasswordsMismatchError"];
                await OnPasswordChange.InvokeAsync(string.Empty);
                StateHasChanged();
                return;
            }

            // If password is valid.
            _isValid = true;
            _errorMessage = string.Empty;

            await OnPasswordChange.InvokeAsync(Password);

            _isValidating = false;
            StateHasChanged();
        });
    }

    /// <summary>
    /// Handles the password input focus.
    /// </summary>
    private void OnPasswordInputFocus(FocusEventArgs args)
    {
        // Reset validation state when the input is focused.
        _isValid = false;
        _isValidating = false;
        _errorMessage = string.Empty;
        StateHasChanged();
    }
}
